package com.xsmart.oa.services;


import com.xsmart.common.constant.ResultConstant;
import com.xsmart.common.entity.RequestBean;
import com.xsmart.common.entity.ResponseBean;
import com.xsmart.oa.dao.UserFavoritesDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 用户收藏
 *
 * @author guojd
 */
@Service
public class UserFavoritesService {
    @Autowired
    UserFavoritesDao userFavoritesDao;

    /**
     * 用户收藏商品
     *
     * @param requestBean 请求对象
     *                    uid：用户id
     *                    pid：商品id
     * @return 是否成功
     */
    public ResponseBean addGoodsUserFavorites(RequestBean requestBean) {
        Integer uid = requestBean.getRequestParamInteger("uid");
        Integer goodsId = requestBean.getRequestParamInteger("pid");
        ResponseBean responseBean = requestBean.getResponseBean();
        //判断是否已经收藏
        List<Map<String, Object>> userFavorites = userFavoritesDao.queryGoodsUserFavorites(null, uid, goodsId);
        if (userFavorites.size() == 0) {
            userFavoritesDao.addGoodsUserFavorites(uid, goodsId);
        }
        responseBean.setResult(ResultConstant.SUCCESS);
        return responseBean;
    }

    /**
     * 查询用户收藏商品
     *
     * @param requestBean 请求对象
     *                    uid：用户id
     *                    pid：商品id
     * @return 是否成功
     */
    public ResponseBean queryGoodsUserFavorites(RequestBean requestBean) {
        Integer uid = requestBean.getRequestParamInteger("uid");
        Integer goodsId = requestBean.getRequestParamInteger("pid");
        ResponseBean responseBean = requestBean.getResponseBean();
        //判断是否已经收藏
        List<Map<String, Object>> userFavorites = userFavoritesDao.queryGoodsUserFavorites(null, uid, goodsId);
        responseBean.setResult(ResultConstant.SUCCESS);
        responseBean.addResultItem("favoritesGoods", userFavorites);
        return responseBean;
    }

    /**
     * 用户取消收藏
     * @param requestBean
     * @return
     */
    public ResponseBean removeGoodsUserFavorites(RequestBean requestBean) {
        Integer ufgId = requestBean.getRequestParamInteger("ufg_id");
        ResponseBean responseBean = requestBean.getResponseBean();
        int userFavorites = userFavoritesDao.removeGoodsUserFavorites(ufgId);
        responseBean.setResult(ResultConstant.SUCCESS);
        responseBean.addResultItem("favoritesGoods", userFavorites);
        return responseBean;
    }

    /**
     * 从商品详情页面中取消收藏
     * @param requestBean
     * @return
     */
    public ResponseBean deleteGoodsUserFavorites(RequestBean requestBean) {
        Integer userId = requestBean.getRequestParamInteger("uid");
        Integer goodsId = requestBean.getRequestParamInteger("pid");
        ResponseBean responseBean = requestBean.getResponseBean();
        int num = userFavoritesDao.deleteGoodsUserFavorites(userId,goodsId);
        responseBean.setResult(ResultConstant.SUCCESS);
        return responseBean;
    }

}
